package com.struts.action.news;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Map;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.spring.service.news.ArticlesLeibieService;
import com.struts.form.ArticlesLeibieForm;

public class ArticlesLeibieAction extends DispatchAction {
	
	private ArticlesLeibieService articlesLeibieService = null;
	
	/**
	 * display article leibie
	 */
	public ActionForward newsLiebeiView(ActionMapping mapping, ActionForm form,
	HttpServletRequest request, HttpServletResponse response){
		Long fatherId=0L;
		if(request.getParameter("fatherId")!=null)
			fatherId=Long.parseLong(request.getParameter("fatherId"));
		request.setAttribute("articlesLiebei", articlesLeibieService.findNewsLeibie("from ArticlesLeibie where fatherId="+fatherId));
		return mapping.findForward("articlesLiebei");
	}
	
	public ActionForward findNewsLeibieTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
				request.setAttribute("articlesLeibieTree", articlesLeibieService.findNewsLeibieTree("from ArticlesLeibie"));
				return mapping.findForward("articlesLeibieTree");
	}
	
	
	public ActionForward findNewsLeibie(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		Long id=0L;
		if(request.getParameter("id")!=null)
		{
			id=Long.parseLong(request.getParameter("id"));
		}

		List list=articlesLeibieService.findNewsLeibie("from ArticlesLeibie where fatherId="+id);
		request.setAttribute("articlesLiebei", list);
		return mapping.findForward("articlesLiebei");
	}
	
	/**
	 * save article leibie
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward saveNewsLeibie(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		ArticlesLeibieForm leibieform=(ArticlesLeibieForm)form;
		if(articlesLeibieService.saveNewsLeibie(leibieform))
			return mapping.findForward("success");
		else
			return mapping.findForward("fail");
	}
	
	/**
	 * delete article leibie
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward deleteNewsLeibie(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		Long id=Long.parseLong(request.getParameter("id"));
		if(articlesLeibieService.deleteNewsLeibie(id))
		{
			return mapping.findForward("success");
		}
		else
		{
			return mapping.findForward("fail");
		}
	}
	
	public ActionForward updateNewsLeibie(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		ArticlesLeibieForm leibieform=(ArticlesLeibieForm)form;
		if(articlesLeibieService.updateNewsLeibie(leibieform))
			return mapping.findForward("success");
		else
			return mapping.findForward("fail");
	}

	public ArticlesLeibieService getArticlesLeibieService() {
		return articlesLeibieService;
	}

	public void setArticlesLeibieService(ArticlesLeibieService articlesLeibieService) {
		this.articlesLeibieService = articlesLeibieService;
	}
}
